﻿using System;

namespace ECommerce.Administration.Controls
{
	public partial class EditLocalStringResource : AdminBaseUsercontrol
	{
		public int LocalStringResourceID { get { return CommonHelper.QueryStringInt("localstringresourceid"); } }

		private Service.ILocalStringResourceService _LocalStringResourceService;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				if (SiteContext.Current == null)
				{
					Server.Transfer("~Administration/AdminLogin.aspx");
				}

				if (LocalStringResourceID > 0)
				{
					LabelEditLSRTitle.Text = GetLocalResourceString("Admin.EditLocalStringResource.Edit.Title");
				}
				else
				{
					LabelEditLSRTitle.Text = GetLocalResourceString("Admin.EditLocalStringResource.Add.Title");
				}

				ButtonSave.Text = GetLocalResourceString("Admin.SaveButtonText");

				SetValues();
			}
		}

		protected void ButtonSave_Click(object sender, EventArgs e)
		{
			if (SiteContext.Current.WorkingLanguage != null)
			{
				Data.Entity.LocalStringResource theLocalStringResource = new Data.Entity.LocalStringResource();
				theLocalStringResource.ResourceName = TextLSRName.Text.Trim();
				theLocalStringResource.ResourceValue = TextLSRValue.Text.Trim();
				theLocalStringResource.LanguageID = SiteContext.Current.WorkingLanguage.LanguageId;

				if (_LocalStringResourceService == null)
				{
					_LocalStringResourceService = new Service.LocalStringResourceService();
				}
				
				if (LocalStringResourceID > 0)
				{
					theLocalStringResource.LocaleStringResourceID = LocalStringResourceID;
					_LocalStringResourceService.UpdateLocaleStringResource(theLocalStringResource);
				}
				else
				{
					_LocalStringResourceService.CreateNewLocaleStringResource(theLocalStringResource);
				}
			}
		}

		private void SetValues()
		{
			if (LocalStringResourceID > 0)
			{
				if (_LocalStringResourceService == null)
				{
					_LocalStringResourceService = new Service.LocalStringResourceService();
				}

				var theLocalStringResource = _LocalStringResourceService.GetTheLocalStringResource(LocalStringResourceID);

				TextLSRName.Text = theLocalStringResource.ResourceName;
				TextLSRValue.Text = theLocalStringResource.ResourceValue;

				TextLSRName.ReadOnly = true;
				TextLSRName.Enabled = true;
			}
		}
	}
}